<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/07/16 17:00
 * Desc: 通用接口
 */
class Controller_Pc_Api_v2_Common extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Common
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Common
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('common', $this->request_params);

        // 服务
        $this->service = new Service_Api_Common();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }


    // 获取图片验证码
    public function action_get_code_image()
    {
        try {
            $this->result['data'] = $this->service->get_code_image();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 图片验证码验证
    public function action_check_code_image()
    {
        try {
            $this->validation->check_code_image()->check();

            $status = $this->service->check_code_image($this->request_params);

            $this->result['status'] = $status;
            $this->result['msg']    = $status ? '验证通过' : '验证失败';
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 发送短信验证码
    public function action_send_msg_code()
    {
        try {
            $this->validation->send_msg_code()->check();

            $status = $this->service->send_msg_code($this->request_params);

            $this->result['status'] = $status;
            $this->result['msg']    = $status ? '发送成功' : '发送失败';
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }


    // 上传图片
    public function action_upload_img()
    {
        try {
            $this->validation->upload_img()->check();

            $this->result['data'] = $this->service->upload_img($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 上传视频
    public function action_upload_video()
    {
        try {
            $this->validation->upload_video()->check();

            $this->result['data'] = $this->service->upload_video($this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }
}